﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data;
using System.Net;

namespace KPIBalanceScoreCard
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            DataRowCollection rows = new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().GetData(username.Text).Rows;

            if (rows != null && rows.Count > 0)
            {
                String password = rows[0]["pwd"].ToString();
                password = Encryption.Decrypt(password, ApplicationStrings.encryptionKey);
                if (!password.Equals(pwd.Text))
                {
                    lblStatus.Text = "Invalid Username or Password";
                    return;
                }
            }
            else
            {
                lblStatus.Text = "Invalid Username or Password";
                return;
            }

            Session[ApplicationConstants.LOGIN_PK.ToString()] = username.Text;
            Session[ApplicationConstants.IS_ADMIN.ToString()] = true;

            Response.Redirect("~/KPIGroupList.aspx");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
           // Session.Clear();
          //  Session[ApplicationConstants.LOGIN_PK.ToString()] = DropDownList2.SelectedValue;
           // Button1.Enabled = true;
           
        }

        protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void forgotPassword_Click(object sender, EventArgs e)
        {
        
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0

        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0

      
        MailMessage message = new MailMessage();
        String email = "";


        try
        {

            try
            {
               email = new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().GetData(username.Text).Rows[0][0].ToString();

            }
            catch (Exception e1)
            {
                
                throw new Exception("Cannot find User in Database!" + e1.Message);
            }
            MailAddress fromAddress = new MailAddress("ritesh.malaiya@gmail.com", "Ritesh Malaiya");

            // You can specify the host name or ipaddress of your server

            // Default in IIS will be localhost 

          
            //Default port will be 25

          
            //From address will be given as a MailAddress Object

            message.From = fromAddress;

            // To address collection of MailAddress

            message.To.Add("ritesh.malaiya@gmail.com");
            message.To.Add(email);

            message.Subject = "Forgot Password Request!!";

            //Body can be Html or text format

            //Specify true if it  is html message

            message.IsBodyHtml = true;

            // Message body content
            DataRowCollection rows = new HierarchyDatasetTableAdapters.Employee_LoginTableAdapter().GetData(username.Text).Rows;
            message.Body = "Dear " + rows[0]["userName"].ToString() +", <br /> Your password is: " + Encryption.Decrypt(rows[0]["pwd"].ToString(), ApplicationStrings.encryptionKey);
         
            // Send SMTP mail

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.UseDefaultCredentials = false;

            smtpClient.EnableSsl = true;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new NetworkCredential("ritesh.malaiya@gmail.com", "Qwer..1234");
            
            smtpClient.Send(message);

            lblStatus.Text = "Email successfully sent to " + email;
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Send Email Failed." + ex.Message;
        }
      
        }
    }
}
